import { FixedLengthArray } from "@utils/build/types";
import * as Cesium from "cesium";
export interface DrawWallType {
  positions: Array<any>;
  color: FixedLengthArray<number, 4>;
  wallHeight: number;
}
class DrawWall {
  #viewer: Cesium.Viewer;
  #bottomP: any[] = [];
  #topP: any[] = [];
  #wallHeight: number = 0;
  #positions: any[] = [];
  #num = 0;
  #length = 0;
  entity: (Cesium.Entity & { data?: any; _node?: any }) | null = null;

  constructor(viewer: Cesium.Viewer, options: DrawWallType) {
    this.#viewer = viewer;
    this.#wallHeight = options.wallHeight || 1000;
    this.#num = options.positions.length - 1;
    this.#length = options.positions.length;
    this.#positions = options.positions;
    this.#init();
  }
  #init() {
    // 顶点坐标
    /**
     * 墙体模型
     * [ 3  4  5 ]
     * [ 0  1  2 ]
     * 三角形顶点坐标示意 [ 0 1 4,  0 4 3,  1 2 5,  1 5 4 ]
     */
    let indices: any[] = [];

    // 纹理坐标 纹理上下层坐标
    let stsBottom = [];
    let stsTop = [];
    for (let i = 0; i < this.#num; i++) {
      // 添加顶点坐标
      let arr = [
        i,
        i + 1,
        i + 1 + this.#length,
        i,
        i + 1 + this.#length,
        i + this.#length,
      ];
      indices = indices.concat(arr);

      // 添加纹理坐标
      stsBottom.push(0.0);
      stsBottom.push(0.0);
      stsTop.push(1.0);
      stsTop.push(1.0);
    }
    // 添加最后一组纹理坐标  使纹理坐标对数与坐标数相等
    stsBottom.push(0.0);
    stsBottom.push(0.0);
    stsTop.push(1.0);
    stsTop.push(1.0);
    let sts = stsBottom.concat(stsTop);

    // 根据坐标  生成上下两组多边形坐标轮廓  将坐标整合用于顶点坐标编织围墙
    this.#positions.forEach((point) => {
      this.#bottomP.push([point[0], point[1], 0]);
      this.#topP.push([point[0], point[1], this.#wallHeight]);
    });
    const positions = this.#bottomP.concat(this.#topP);
    this.entity = this.#viewer.entities.add({});
    this.entity._node = this;
  }
  destroy() {}
  setShow() {}
}
export { DrawWall };
